package budo.http;

import org.budo.dubbo.protocol.http.exporter.BudoDubboHttpExporter;
import org.budo.support.lang.util.ReflectUtil;
import org.budo.support.spring.util.SpringUtil;

import lombok.Getter;
import lombok.Setter;

/**
 * @author lmw
 */
@Getter
@Setter
public class Service extends BudoDubboHttpExporter {
    private String interfaceTypeName;

    private String interfaceInstanceName;

    /**
     * 传入接口
     * 
     * @param interfaceType
     */
    public void setInterfaceType(Class<?> interfaceType) {
        this.setInterfaceTypes(new Class<?>[] { interfaceType });
    }

    /**
     * 传入接口
     * 
     * @param interfaceType
     */
    public void setInterfaceTypeName(String interfaceTypeName) {
        if (ReflectUtil.hasClass(interfaceTypeName)) {
            this.setInterfaceTypes(new Class<?>[] { ReflectUtil.classForName(interfaceTypeName) });
            return;
        }

        this.interfaceTypeName = interfaceTypeName;
    }

    protected void exportService() {
        super.exportService();

        if (null != this.getInterfaceTypeName() && null != this.getInterfaceInstanceName()) {
            Object beanInstance = SpringUtil.getBean(this.getInterfaceInstanceName());
            this.exportService(this.getInterfaceTypeName(), beanInstance);
        }
    }

    @Override
    public String toString() {
        return super.toString() + ", interfaceInstanceName=" + this.getInterfaceTypeName() + ", interfaceInstanceName=" + this.getInterfaceInstanceName();
    }
}